#!/bin/bash
#
# https://github.com/Aniverse/inexistence
# Author: Aniverse

script_update=2020.03.02
script_version=r11010

################################################################################################ Debug

usage_for_debug() {
    export local_packages=/etc/inexistence/00.Installation
    export s=$local_packages/package/rclone/install
    rm -f $s ; nano $s ; bash $s
    bash <(wget -qO- https://github.com/Aniverse/inexistence/raw/master/00.Installation/package/rclone/install)
}

################################################################################################ Get options

unset LogRootPath LogTimes DebLocation SCLocation LOCKLocation OutputLOG

AppName=rclone
AppNameLower=rclone
Need_SourceCode=no
pm_action=install

function show_usage() { echo "
$AppName $pm_action $script_version ($script_update)
Usage:
      -v        Specifiy which version to be installed
      -l        Specifiy LogBase
      -d        Enable debug mode
"
exit 1 ; }

OPTS=$(getopt -o v:l:d -al version:,debug,logbase:,log -- "$@")
[ ! $? = 0 ] && show_usage
eval set -- "$OPTS"

while [ -n "$1" ] ; do case "$1" in
    -v | --version      ) version="$2"  ; shift 2 ;;
    -l | --logbase      ) LogTimes="$2" ; shift 2 ;;
    -d | --debug        ) debug=1       ; shift   ;;
         --log          ) show_log=1    ; shift   ;;
    -- ) shift ; break ;;
esac ; done

################################################################################################ Set Variables

if [[ -f /etc/inexistence/00.Installation/function ]]; then
    source /etc/inexistence/00.Installation/function
else
    source <(wget -qO- https://github.com/Aniverse/inexistence/raw/master/00.Installation/function)
fi

set_variables_log_location
check_var_OutputLOG
debug_log_location
cat_outputlog

################################################################################################




function install_rclone() {
    echo >> $OutputLOG
    cd /tmp

    wget -nv https://downloads.rclone.org/rclone-current-linux-amd64.zip -O rclone-current-linux-amd64.zip >> $OutputLOG 2>&1
    unzip rclone-current-linux-amd64.zip  >> $OutputLOG 2>&1
    cd rclone-*-linux-amd64               >> $OutputLOG 2>&1
    cp rclone /usr/bin/                   >> $OutputLOG 2>&1
    chown root:root /usr/bin/rclone       >> $OutputLOG 2>&1
    chmod 755 /usr/bin/rclone             >> $OutputLOG 2>&1
    mkdir -p /usr/local/share/man/man1    >> $OutputLOG 2>&1
    cp rclone.1 /usr/local/share/man/man1 >> $OutputLOG 2>&1
    mandb                                 >> $OutputLOG 2>&1
    cd ..
    rm -rf rclone-*-linux-amd64 rclone-current-linux-amd64.zip

    status_lock=$AppNameLower
    echo "status_lock=$status_lock" > /tmp/Variables
    rm -f /tmp/$status_lock.1.lock /tmp/$status_lock.2.lock 
    which rclone > /dev/null && touch /tmp/$status_lock.1.lock || touch /tmp/$status_lock.2.lock
    [[ -f /tmp/$status_lock.1.lock ]] && touch $LOCKLocation/$AppNameLower.lock
    echo >> $OutputLOG
}




echo_task "Installing $AppName ..."
install_rclone & spinner $!
check_status $status_lock
